
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>php画图 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="07_php加密机制.html" />
    
    
    <link rel="prev" href="05_session.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../php基础/01_php介绍.html">
            
                <a href="../php基础/01_php介绍.html">
            
                    
                    php基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../php基础/01_php介绍.html">
            
                <a href="../php基础/01_php介绍.html">
            
                    
                    php介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../php基础/02_PHP基础语法.html">
            
                <a href="../php基础/02_PHP基础语法.html">
            
                    
                    php基础语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../php基础/03_php变量.html">
            
                <a href="../php基础/03_php变量.html">
            
                    
                    php变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../php基础/04_php数据类型.html">
            
                <a href="../php基础/04_php数据类型.html">
            
                    
                    php数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../php基础/05_php常量.html">
            
                <a href="../php基础/05_php常量.html">
            
                    
                    php常量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../php基础/06_php运算符.html">
            
                <a href="../php基础/06_php运算符.html">
            
                    
                    php运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../php基础/07_php流程控制.html">
            
                <a href="../php基础/07_php流程控制.html">
            
                    
                    php流程控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="../php基础/08_php数组.html">
            
                <a href="../php基础/08_php数组.html">
            
                    
                    php数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="../php基础/09_php函数.html">
            
                <a href="../php基础/09_php函数.html">
            
                    
                    php函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10" data-path="../php基础/10_php面向对象.html">
            
                <a href="../php基础/10_php面向对象.html">
            
                    
                    php面向对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.11" data-path="../php基础/11_php命名空间.html">
            
                <a href="../php基础/11_php命名空间.html">
            
                    
                    php命名空间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.12" data-path="../php基础/12_php日期和时间.html">
            
                <a href="../php基础/12_php日期和时间.html">
            
                    
                    php日期时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.13" data-path="../php基础/13_php表单.html">
            
                <a href="../php基础/13_php表单.html">
            
                    
                    php表单
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.14" data-path="../php基础/14_php包含文件.html">
            
                <a href="../php基础/14_php包含文件.html">
            
                    
                    php包含文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.15" data-path="../php基础/15_php常用函数.html">
            
                <a href="../php基础/15_php常用函数.html">
            
                    
                    php常用函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.16" data-path="16_php常用变量函数.md">
            
                <span>
            
                    
                    php常用变量函数
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="01_php错误与异常.html">
            
                <a href="01_php错误与异常.html">
            
                    
                    php错误与异常
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="02_PhpStorm本地断点调试.html">
            
                <a href="02_PhpStorm本地断点调试.html">
            
                    
                    PhpStorm本地断点调试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="03_php文件操作.html">
            
                <a href="03_php文件操作.html">
            
                    
                    php文件操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="04_cookie机制.html">
            
                <a href="04_cookie机制.html">
            
                    
                    cookie机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="05_session.html">
            
                <a href="05_session.html">
            
                    
                    session
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.5" data-path="06_php画图.html">
            
                <a href="06_php画图.html">
            
                    
                    php画图
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="07_php加密机制.html">
            
                <a href="07_php加密机制.html">
            
                    
                    php加密机制
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >php画图</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="php&#x753B;&#x56FE;">PHP&#x753B;&#x56FE;</h1>
<blockquote>
<p>&#x5728;PHP&#x4E2D;,&#x6709;&#x4E9B;&#x529F;&#x80FD;&#x662F;&#x6838;&#x5FC3;&#x529F;&#x80FD;,&#x5982;&#x6570;&#x7EC4;&#x53CA;&#x6570;&#x7EC4;&#x51FD;&#x6570;,&#x9ED8;&#x8BA4;&#x5C31;&#x6709;&#x8FD9;&#x4E9B;&#x51FD;&#x6570;; &#x6709;&#x4E00;&#x4E9B;&#x529F;&#x80FD;&#x5219;&#x662F;&#x6269;&#x5C55;&#x529F;&#x80FD;,&#x9700;&#x8981;&#x5F15;&#x5165;&#x6269;&#x5C55;&#x624D;&#x80FD;&#x7528;,&#x5982;mysql_*&#x51FD;&#x6570;,pdo,gd</p>
</blockquote>
<p>PHP &#x4E0D;&#x4EC5;&#x9650;&#x4E8E;&#x53EA;&#x4EA7;&#x751F; HTML &#x7684;&#x8F93;&#x51FA;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x521B;&#x5EFA;&#x53CA;&#x64CD;&#x4F5C;&#x591A;&#x79CD;&#x4E0D;&#x540C;&#x683C;&#x5F0F;&#x7684;&#x56FE;&#x50CF;&#x6587;&#x4EF6;&#x3002;PHP&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E9B;&#x5185;&#x7F6E;&#x7684;&#x56FE;&#x50CF;&#x4FE1;&#x606F;&#x51FD;&#x6570;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;GD&#x51FD;&#x6570;&#x5E93;&#x521B;&#x5EFA;&#x65B0;&#x56FE;&#x50CF;&#x6216;&#x5904;&#x7406;&#x5DF2;&#x6709;&#x7684;&#x56FE;&#x50CF;&#x3002;&#x76EE;&#x524D;GD2&#x5E93;&#x652F;&#x6301;GIF&#x3001;JPEG&#x3001;PNG&#x548C;WBMP&#x7B49;&#x683C;&#x5F0F;&#x3002;&#x6B64;&#x5916;&#x8FD8;&#x652F;&#x6301;&#x4E00;&#x4E9B;FreeType&#xFF08;&#x52A0;&#x8F7D;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x4E2D;&#x7684;&#x5B57;&#x4F53;&#xFF09;&#x3001;Type1&#x7B49;&#x5B57;&#x4F53;&#x5E93;&#x3002;</p>
<h2 id="gd&#x5E93;">GD&#x5E93;</h2>
<p>GD&#x5E93;&#xFF0C;&#x662F;php&#x5904;&#x7406;&#x56FE;&#x5F62;&#x7684;&#x6269;&#x5C55;&#x5E93;&#xFF0C;GD&#x5E93;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x7CFB;&#x5217;&#x7528;&#x6765;&#x5904;&#x7406;&#x56FE;&#x7247;&#x7684;API&#xFF0C;&#x4F7F;&#x7528;GD&#x5E93;&#x53EF;&#x4EE5;&#x5904;&#x7406;&#x56FE;&#x7247;&#xFF0C;&#x6216;&#x8005;&#x751F;&#x6210;&#x56FE;&#x7247;&#x3002;</p>
<p>&#x901A;&#x8FC7;GD&#x5E93;&#x4E2D;&#x7684;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x5B8C;&#x6210;&#x5404;&#x79CD;&#x70B9;&#x3001;&#x7EBF;&#x3001;&#x51E0;&#x4F55;&#x56FE;&#x5F62;&#x3001;&#x6587;&#x672C;&#x53CA;&#x989C;&#x8272;&#x7684;&#x64CD;&#x4F5C;&#x548C;&#x5904;&#x7406;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x521B;&#x5EFA;&#x6216;&#x8BFB;&#x53D6;&#x591A;&#x79CD;&#x683C;&#x5F0F;&#x7684;&#x56FE;&#x50CF;&#x6587;&#x4EF6;&#x3002;</p>
<ol>
<li>&#x65B0;&#x5EFA;&#x7A7A;&#x767D;&#x753B;&#x5E03;(&#x6307;&#x5B9A;&#x5BBD;&#x9AD8;)</li>
<li>&#x521B;&#x5EFA;&#x989C;&#x6599;</li>
<li>&#x753B;&#x56FE;&#x5F62;(&#x692D;&#x5706;,&#x77E9;&#x5F62;,&#x76F4;&#x7EBF;&#x7B49;),&#x6216;&#x5199;&#x5B57;</li>
<li>&#x8F93;&#x51FA;/&#x4FDD;&#x5B58;&#x56FE;&#x5F62;</li>
<li>&#x9500;&#x6BC1;&#x753B;&#x5E03;(&#x5173;&#x95ED;&#x753B;&#x677F;)</li>
</ol>
<h2 id="&#x4F7F;&#x7528;&#x8BE6;&#x89E3;">&#x4F7F;&#x7528;&#x8BE6;&#x89E3;</h2>
<h3 id="&#x521B;&#x5EFA;&#x753B;&#x5E03;">&#x521B;&#x5EFA;&#x753B;&#x5E03;</h3>
<p>&#x7528;PHP&#x7684;GD&#x5E93;&#x5904;&#x7406;&#x56FE;&#x50CF;&#x65F6;&#xFF0C;&#x5FC5;&#x987B;&#x5BF9;&#x753B;&#x5E03;&#x8FDB;&#x884C;&#x7BA1;&#x7406;&#x3002;&#x521B;&#x5EFA;&#x753B;&#x5E03;&#x5C31;&#x662F;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#x5F00;&#x8F9F;&#x4E00;&#x5757;&#x5B58;&#x50A8;&#x533A;&#x57DF;&#xFF0C;&#x4EE5;&#x540E;&#x5728;PHP&#x4E2D;&#x5BF9;&#x56FE;&#x50CF;&#x7684;&#x6240;&#x6709;&#x64CD;&#x4F5C;&#x90FD;&#x662F;&#x57FA;&#x4E8E;&#x8FD9;&#x4E2A;&#x56FE;&#x5E03;&#x5904;&#x7406;&#x7684;&#xFF0C;&#x56FE;&#x5E03;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x56FE;&#x50CF;&#x8D44;&#x6E90;&#x3002;</p>
<p>&#x5728;PHP&#x4E2D;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;imagecreate()&#x548C;imageCreateTrueColor()&#x4E24;&#x4E2A;&#x51FD;&#x6570;&#x521B;&#x5EFA;&#x6307;&#x5B9A;&#x7684;&#x753B;&#x5E03;&#x3002;&#x8FD9;&#x4E24;&#x4E2A;&#x51FD;&#x6570;&#x7684;&#x4F5C;&#x7528;&#x662F;&#x4E00;&#x81F4;&#x7684;&#xFF0C;&#x90FD;&#x662F;&#x5EFA;&#x7ACB;&#x4E00;&#x4E2A;&#x6307;&#x5B9A;&#x5927;&#x5C0F;&#x7684;&#x753B;&#x5E03;&#xFF0C;&#x5B83;&#x4EEC;&#x7684;&#x539F;&#x578B;&#x5982;&#x4E0B;&#x6240;&#x793A;&#xFF1A;</p>
<pre><code class="lang-php"><span class="hljs-comment"># &#x65B0;&#x5EFA;&#x4E00;&#x4E2A;&#x57FA;&#x4E8E;&#x8C03;&#x8272;&#x677F;&#x7684;&#x56FE;&#x50CF;</span>
resource imagecreate(int $x_size,int $y_size);

<span class="hljs-comment"># &#x65B0;&#x5EFA;&#x4E00;&#x4E2A;&#x771F;&#x5F69;&#x8272;&#x56FE;&#x50CF;</span>
resource imagecreatetruecolor(int $x_size,int $y_size);
</code></pre>
<p>&#x5F53;&#x753B;&#x5E03;&#x521B;&#x5EFA;&#x540E;&#xFF0C;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x56FE;&#x50CF;&#x6807;&#x8BC6;&#x7B26;&#xFF0C;&#x4EE3;&#x8868;&#x4E86;&#x4E00;&#x5E45;&#x5BBD;&#x5EA6;&#x4E3A;$x_size&#x548C;&#x9AD8;&#x5EA6;&#x4E3A;$y_size&#x7684;&#x7A7A;&#x767D;&#x56FE;&#x50CF;&#x5F15;&#x7528;&#x53E5;&#x67C4;&#x3002;&#x5728;&#x540E;&#x7EED;&#x7684;&#x7ED8;&#x56FE;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x90FD;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x8D44;&#x6E90;&#x7C7B;&#x578B;&#x7684;&#x53E5;&#x67C4;&#x3002;</p>
<p>&#x4F8B;&#x5982;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x8C03;&#x7528;imagex() &#x548C; imagey() &#x4E24;&#x4E2A;&#x51FD;&#x6570;&#x83B7;&#x53D6;&#x56FE;&#x7247;&#x7684;&#x5927;&#x5C0F;&#x3002;</p>
<pre><code class="lang-php">$img = imgcreatetruecolor(<span class="hljs-number">300</span>,<span class="hljs-number">200</span>); <span class="hljs-comment">//&#x521B;&#x5EFA;&#x4E00;&#x4E2A;300*200&#x7684;&#x753B;&#x5E03;</span>
<span class="hljs-keyword">echo</span> imagesx($img); <span class="hljs-comment">//&#x8F93;&#x51FA;&#x753B;&#x5E03;&#x5BBD;&#x5EA6; 300</span>
<span class="hljs-keyword">echo</span> imagesy($img); <span class="hljs-comment">//&#x8F93;&#x51FA;&#x753B;&#x5E03;&#x9AD8;&#x5EA6; 200</span>
</code></pre>
<h3 id="&#x9500;&#x6BC1;&#x753B;&#x5E03;">&#x9500;&#x6BC1;&#x753B;&#x5E03;</h3>
<p>&#x753B;&#x5E03;&#x7684;&#x5F15;&#x7528;&#x53E5;&#x67C4;&#x5982;&#x679C;&#x4E0D;&#x518D;&#x4F7F;&#x7528;&#xFF0C;&#x4E00;&#x5B9A;&#x8981;&#x5C06;&#x8FD9;&#x4E2A;&#x8D44;&#x6E90;&#x9500;&#x6BC1;&#xFF0C;&#x91CA;&#x653E;&#x5185;&#x5B58;&#x4E0E;&#x8BE5;&#x56FE;&#x50CF;&#x7684;&#x5B58;&#x50A8;&#x5355;&#x5143;&#x3002;</p>
<pre><code class="lang-php"><span class="hljs-comment"># &#x9500;&#x6BC1;&#x56FE;&#x50CF;</span>
bool imagedestroy(resource $image)
</code></pre>
<h3 id="&#x8BBE;&#x7F6E;&#x989C;&#x8272;">&#x8BBE;&#x7F6E;&#x989C;&#x8272;</h3>
<p>&#x7F8E;&#x4E3D;&#x7684;&#x56FE;&#x50CF;&#xFF0C;&#x79BB;&#x4E0D;&#x5F00;&#x8272;&#x5F69;&#x3002;&#x5728;PHP&#x4E2D;&#x8BBE;&#x7F6E;&#x56FE;&#x50CF;&#x4E2D;&#x7684;&#x989C;&#x8272;&#xFF0C;&#x9700;&#x8981;&#x8C03;&#x7528;imagecolorallocate() &#x51FD;&#x6570;&#x5B8C;&#x6210;&#x3002;&#x5982;&#x679C;&#x5728;&#x56FE;&#x50CF;&#x4E2D;&#x9700;&#x8981;&#x8BBE;&#x7F6E;&#x591A;&#x79CD;&#x989C;&#x8272;&#xFF0C;&#x53EA;&#x8981;&#x591A;&#x6B21;&#x8C03;&#x7528;&#x8BE5;&#x51FD;&#x6570;&#x5373;&#x53EF;&#x3002;</p>
<pre><code>int imagecolorallocate(resource $image,int $red,int $green,int $blue); //&#x5206;&#x914D;&#x989C;&#x8272;
</code></pre><p>&#x8BE5;&#x51FD;&#x6570;&#x4F1A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x6807;&#x8BC6;&#x7B26;&#xFF0C;&#x4EE3;&#x8868;&#x4E86;&#x7531;&#x7ED9;&#x5B9A;&#x7684;RGB&#x7EC4;&#x6210;&#x7684;&#x989C;&#x8272;&#x3002;&#x53C2;&#x6570;&#x662F;0-255&#x7684;&#x6574;&#x6570;&#x6216;&#x5341;&#x516D;&#x8FDB;&#x5236;&#x7684;0x00 - 0xFF&#x3002;</p>
<pre><code class="lang-php">$img = imagecreate(<span class="hljs-number">100</span>,<span class="hljs-number">100</span>);

<span class="hljs-comment">//rgb&#x8BBE;&#x7F6E;&#x989C;&#x8272;</span>
$bgColor = imagecolorallocate($img,<span class="hljs-number">255</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>);
$white = imagecolorallocate($img,<span class="hljs-number">255</span>,<span class="hljs-number">255</span>,<span class="hljs-number">255</span>);

<span class="hljs-comment">//&#x5341;&#x516D;&#x8FDB;&#x5236;&#x65B9;&#x5F0F;</span>
$white = imagecolorallocate($img,<span class="hljs-number">0xFF</span>,<span class="hljs-number">0xFF</span>,<span class="hljs-number">0xFF</span>);
$black = imagecolorallocate($img,<span class="hljs-number">0x00</span>,<span class="hljs-number">0x00</span>,<span class="hljs-number">0x00</span>);
</code></pre>
<p>&#x5BF9;&#x4E8E;&#x7528; <code>imagecreate()</code> &#x5EFA;&#x7ACB;&#x7684;&#x56FE;&#x50CF;&#xFF0C;&#x7B2C;&#x4E00;&#x6B21;&#x8C03;&#x7528; <code>imagecolorallocate()</code> &#x4F1A;&#x7ED9;&#x56FE;&#x50CF;&#x586B;&#x5145;&#x80CC;&#x666F;&#x3002; &#x5BF9;&#x4E8E;&#x7528; <code>imagecreatetruecolor()</code> &#x5EFA;&#x7ACB;&#x7684;&#x56FE;&#x50CF;&#xFF0C;&#x5219;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x522B;&#x7684;&#x6307;&#x4EE4;&#x5982; <code>imagefill()</code> &#x586B;&#x5145;&#x80CC;&#x666F;&#x3002;</p>
<h3 id="&#x751F;&#x6210;&#x56FE;&#x50CF;">&#x751F;&#x6210;&#x56FE;&#x50CF;</h3>
<p>&#x4F7F;&#x7528;GD&#x5E93;&#x4E2D;&#x63D0;&#x4F9B;&#x7684;&#x51FD;&#x6570;&#x52A8;&#x6001;&#x7ED8;&#x5236;&#x5B8C;&#x6210;&#x56FE;&#x50CF;&#x4EE5;&#x540E;&#xFF0C;&#x5C31;&#x9700;&#x8981;&#x8F93;&#x51FA;&#x5230;&#x6D4F;&#x89C8;&#x5668;&#x6216;&#x8005;&#x5C06;&#x56FE;&#x50CF;&#x4FDD;&#x5B58;&#x8D77;&#x6765;&#x3002;&#x5728;PHP&#x4E2D;&#xFF0C;&#x53EF;&#x4EE5;&#x5C06;&#x52A8;&#x6001;&#x7ED8;&#x5236;&#x5B8C;&#x6210;&#x7684;&#x753B;&#x5E03;&#xFF0C;&#x76F4;&#x63A5;&#x751F;&#x6210;GIF&#x3001;JPEG&#x3001;PNG&#x548C;WBMP&#x56DB;&#x79CD;&#x56FE;&#x50CF;&#x683C;&#x5F0F;&#x3002;</p>
<pre><code>bool imagegif(resource $image[,string $filename]); //&#x4EE5;GIF&#x683C;&#x5F0F;&#x5C06;&#x56FE;&#x50CF;&#x8F93;&#x51FA;
bool imagejpeg(resource $image[,string $filename[,int $quality]]); //&#x4EE5;JPEG&#x683C;&#x5F0F;&#x5C06;&#x56FE;&#x50CF;&#x8F93;&#x51FA;
bool imagepng(resource $image[,string $filename]); //&#x4EE5;PNG&#x683C;&#x5F0F;&#x5C06;&#x56FE;&#x50CF;&#x8F93;&#x51FA;
bool imagewbmp(resource $image[,string $filename[,int $foreground]]); //&#x4EE5;WBMP&#x683C;&#x5F0F;&#x5C06;&#x56FE;&#x50CF;&#x8F93;&#x51FA;
</code></pre><p>&#x5F53;&#x8BBF;&#x95EE;&#x65F6;&#x5219;&#x76F4;&#x63A5;&#x5C06;&#x539F;&#x56FE;&#x50CF;&#x6D41;&#x8F93;&#x51FA;&#xFF0C;&#x5E76;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x4E2D;&#x663E;&#x793A;&#x52A8;&#x6001;&#x8F93;&#x51FA;&#x7684;&#x56FE;&#x50CF;&#x3002;&#x4F46;&#x662F;&#x4E00;&#x5B9A;&#x8981;&#x5728;&#x8F93;&#x51FA;&#x4E4B;&#x524D;&#xFF0C;&#x4F7F;&#x7528;header()&#x51FD;&#x6570;&#x53D1;&#x9001;http&#x5934;&#x4FE1;&#x606F;&#xFF0C;&#x6765;&#x901A;&#x77E5;&#x6D4F;&#x89C8;&#x5668;&#x4F7F;&#x7528;&#x6B63;&#x786E;&#x7684;MIME&#x7C7B;&#x578B;&#x5BF9;&#x63A5;&#x53D7;&#x7684;&#x5185;&#x5BB9;&#x8FDB;&#x884C;&#x89E3;&#x6790;&#xFF0C;&#x8BA9;&#x6D4F;&#x89C8;&#x5668;&#x77E5;&#x9053;&#x6211;&#x4EEC;&#x53D1;&#x9001;&#x7684;&#x662F;&#x56FE;&#x7247;&#x3002;</p>
<pre><code class="lang-php">$img = imagecreate(<span class="hljs-number">100</span>,<span class="hljs-number">100</span>);

<span class="hljs-comment"># &#x53D1;&#x9001;git&#x56FE;&#x7247;</span>
header(<span class="hljs-string">&quot;Content-type:image/gif&quot;</span>);
imagegif($img);

<span class="hljs-comment"># &#x53D1;&#x9001;jpeg&#x56FE;&#x7247;</span>
header(<span class="hljs-string">&quot;Content-type:image/jpeg&quot;</span>);
imagejpeg($img);

<span class="hljs-comment"># &#x53D1;&#x9001;png&#x56FE;&#x7247;</span>
header(<span class="hljs-string">&quot;Content-type:image/png&quot;</span>);
imagepng($img);

<span class="hljs-comment"># &#x53D1;&#x9001;wbmp&#x56FE;&#x7247;</span>
header(<span class="hljs-string">&quot;Content-type:image/vnd.wap.wbmp&quot;</span>);
imagewbmp($img);
</code></pre>
<h2 id="&#x7ED8;&#x5236;&#x51FD;&#x6570;">&#x7ED8;&#x5236;&#x51FD;&#x6570;</h2>
<h3 id="&#x586B;&#x5145;&#x533A;&#x57DF;">&#x586B;&#x5145;&#x533A;&#x57DF;</h3>
<pre><code class="lang-php">bool imagefill( resource $image , int $x , int $y , int $color )
</code></pre>
<p>imagefill()&#x5728;image&#x56FE;&#x50CF;&#x7684;(x,y)&#x5904;&#x7528; color&#x989C;&#x8272;&#x6267;&#x884C;&#x533A;&#x57DF;&#x586B;&#x5145;&#xFF08;&#x5373;&#x4E0E; (x, y) &#x70B9;&#x989C;&#x8272;&#x76F8;&#x540C;&#x4E14;&#x76F8;&#x90BB;&#x7684;&#x70B9;&#x90FD;&#x4F1A;&#x88AB;&#x586B;&#x5145;&#xFF09;</p>
<h3 id="&#x753B;&#x70B9;">&#x753B;&#x70B9;</h3>
<pre><code class="lang-php">bool imagesetpixel( resource $image , int $x , int $y , int $color )
</code></pre>
<h3 id="&#x753B;&#x7EBF;">&#x753B;&#x7EBF;</h3>
<pre><code class="lang-php">bool imageline( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
</code></pre>
<p>&#x4ECE;(x1, y1)&#x5230;(x2,y2)&#x3002;&#x7EBF;&#x7684;&#x98CE;&#x683C;&#x53EF;&#x4EE5;&#x7531;<code>bool imagesetstyle( resource $image , array $style )</code>&#x6765;&#x63A7;&#x5236;&#x3002;&#x5BBD;&#x5EA6;&#x7531;<code>bool imagesetthickness ( resource $image , int $thickness )</code>&#x63A7;&#x5236;&#xFF0C;&#x6CE8;&#x610F;&#x8FD9;&#x4E2A;&#x5BBD;&#x5EA6;&#x5728;&#x753B;&#x77E9;&#x5F62;&#x3001;&#x5F27;&#x7EBF;&#x65F6;&#x4E5F;&#x751F;&#x6548;&#x3002;</p>
<h3 id="&#x5B57;&#x7B26;">&#x5B57;&#x7B26;</h3>
<p>&#x6C34;&#x5E73;&#x5730;&#x753B;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;</p>
<pre><code>bool imagechar(resource $image , int $font , int $x , int $y , string $c , int $color)
</code></pre><p>&#x5782;&#x76F4;&#x5730;&#x753B;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;</p>
<pre><code>bool imagecharup(resource $image , int $font , int $x , int $y , string $c , int $color)
</code></pre><p>&#x6C34;&#x5E73;&#x5730;&#x753B;&#x4E00;&#x884C;&#x5B57;&#x7B26;&#x4E32;</p>
<pre><code>bool imagestring(resource $image , int $font , int $x , int $y , string $s , int $col)
</code></pre><p>&#x5782;&#x76F4;&#x5730;&#x753B;&#x4E00;&#x884C;&#x5B57;&#x7B26;&#x4E32;</p>
<pre><code>bool imagestringup(resource $image , int $font , int $x , int $y , string $s , int $col)
</code></pre><p>$font&#x53C2;&#x6570;&#x8981;&#x6CE8;&#x610F;&#x4E0B;&#xFF0C;&#x8981;&#x4E48;&#x4F7F;&#x7528;&#x5185;&#x7F6E;&#x7684;&#x5B57;&#x4F53;&#xFF08;&#x4ECE;1&#x5230;5&#xFF09;&#xFF0C;&#x8981;&#x4E48;&#x7528;<code>int imageloadfont ( string $file )</code>&#x52A0;&#x8F7D;&#x5B57;&#x4F53;&#x540E;&#x518D;&#x8BBE;&#x7F6E;&#x3002;</p>
<h3 id="&#x7ED8;&#x5236;&#x5F62;&#x72B6;">&#x7ED8;&#x5236;&#x5F62;&#x72B6;</h3>
<p>&#x77E9;&#x5F62;</p>
<pre><code>bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col )
</code></pre><p>&#x692D;&#x5706;</p>
<pre><code>bool imageellipse ( resource $image , int $cx , int $cy , int $w , int $h , int $color )
</code></pre><p>&#x591A;&#x8FB9;&#x5F62;</p>
<pre><code>bool imagepolygon ( resource $image , array $points , int $num_points , int $color )
</code></pre><h2 id="&#x9A8C;&#x8BC1;&#x7801;">&#x9A8C;&#x8BC1;&#x7801;</h2>
<pre><code class="lang-php"><span class="hljs-meta">&lt;?php</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Code</span></span>{
    <span class="hljs-keyword">public</span> $width = <span class="hljs-number">120</span>; <span class="hljs-comment">//&#x5BBD;&#x5EA6;</span>
    <span class="hljs-keyword">public</span> $height = <span class="hljs-number">60</span>; <span class="hljs-comment">//&#x9AD8;&#x5EA6;</span>
    <span class="hljs-keyword">public</span> $nums = <span class="hljs-number">4</span>;    <span class="hljs-comment">//&#x9A8C;&#x8BC1;&#x7801;&#x6570;&#x91CF;</span>
    <span class="hljs-keyword">public</span> $font_file = <span class="hljs-string">&apos;./font/&#x5FAE;&#x8F6F;&#x96C5;&#x9ED1;+Arial.ttf&apos;</span>; <span class="hljs-comment">//&#x5B57;&#x4F53;&#x4F4D;&#x7F6E;</span>
    <span class="hljs-keyword">public</span> $font_size = <span class="hljs-keyword">array</span>(<span class="hljs-string">&apos;min&apos;</span>=&gt;<span class="hljs-number">20</span>,<span class="hljs-string">&apos;max&apos;</span>=&gt;<span class="hljs-number">26</span>);
    <span class="hljs-keyword">public</span> $pointer_num = <span class="hljs-number">200</span>;
    <span class="hljs-keyword">private</span> $pic = <span class="hljs-keyword">null</span>;
    <span class="hljs-keyword">private</span> $letter = <span class="hljs-string">&quot;abcdeB6DfEg7FVBh5KLiHUT8Mg4kAl3mCa9oT1pqR0r2OstIuvPQwxyz&quot;</span>;

    <span class="hljs-comment">//&#x83B7;&#x53D6;&#x56FE;&#x7247;</span>
    <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createPic</span><span class="hljs-params">()</span></span>{
        $this-&gt;pic = imagecreatetruecolor($this-&gt;width,$this-&gt;height);
    }

    <span class="hljs-comment">//&#x586B;&#x5145;&#x80CC;&#x666F;&#x8272;</span>
    <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fillBg</span><span class="hljs-params">()</span></span>{
        $color = imagecolorallocate($this-&gt;pic,rand(<span class="hljs-number">0</span>,<span class="hljs-number">127</span>),rand(<span class="hljs-number">0</span>,<span class="hljs-number">127</span>),rand(<span class="hljs-number">0</span>,<span class="hljs-number">127</span>));
        imagefill($this-&gt;pic,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,$color);
    }
    <span class="hljs-comment">//&#x83B7;&#x53D6;&#x968F;&#x673A;&#x4F4D;&#x7F6E;</span>
    <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getPos</span><span class="hljs-params">()</span></span>{
        $w = $this-&gt;width;
        $h = $this-&gt;height;
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">array</span>(<span class="hljs-string">&apos;x&apos;</span>=&gt;rand(<span class="hljs-number">0</span>,$w),<span class="hljs-string">&apos;y&apos;</span>=&gt;rand(<span class="hljs-number">0</span>,$h));
    }
    <span class="hljs-comment">//&#x586B;&#x5145;&#x70B9;</span>
    <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fillPixel</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">for</span> ($i=<span class="hljs-number">0</span>;$i&lt;$this-&gt;pointer_num;$i++){
            $color = imagecolorallocate($this-&gt;pic,rand(<span class="hljs-number">0</span>,<span class="hljs-number">255</span>),rand(<span class="hljs-number">0</span>,<span class="hljs-number">255</span>),rand(<span class="hljs-number">0</span>,<span class="hljs-number">255</span>));
            $pos = $this-&gt;getPos();
            imagesetpixel($this-&gt;pic,$pos[<span class="hljs-string">&apos;x&apos;</span>],$pos[<span class="hljs-string">&apos;y&apos;</span>],$color);
        }
    }
    <span class="hljs-comment">//&#x586B;&#x5145;&#x7EBF;&#x6761;</span>
    <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fillLine</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">for</span> ($i=<span class="hljs-number">0</span>;$i&lt;<span class="hljs-number">10</span>;$i++){
            $color = imagecolorallocate($this-&gt;pic,rand(<span class="hljs-number">0</span>,<span class="hljs-number">127</span>),rand(<span class="hljs-number">0</span>,<span class="hljs-number">127</span>),rand(<span class="hljs-number">0</span>,<span class="hljs-number">127</span>));
            $p1 = $this-&gt;getPos();
            $p2 = $this-&gt;getPos();
            imageline($this-&gt;pic,$p1[<span class="hljs-string">&apos;x&apos;</span>],$p1[<span class="hljs-string">&apos;y&apos;</span>],$p2[<span class="hljs-string">&apos;x&apos;</span>],$p2[<span class="hljs-string">&apos;y&apos;</span>],$color);
        }
    }
    <span class="hljs-comment">//&#x586B;&#x5145;&#x6587;&#x5B57;</span>
    <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fillText</span><span class="hljs-params">()</span></span>{
        $len = strlen($this-&gt;letter);
        $ww = $this-&gt;width/$this-&gt;nums;
        $hh = $this-&gt;height/<span class="hljs-number">2</span>;
        $fonts = <span class="hljs-keyword">array</span>();
        <span class="hljs-keyword">for</span> ($i=<span class="hljs-number">0</span>;$i&lt;$this-&gt;nums;$i++){
            $l = substr($this-&gt;letter,rand(<span class="hljs-number">0</span>,$len),<span class="hljs-number">1</span>);
            <span class="hljs-keyword">while</span> (in_array($l,$fonts)){
                $l = substr($this-&gt;letter,rand(<span class="hljs-number">0</span>,$len),<span class="hljs-number">1</span>);
            }
            $fonts[] = $l;
        }
        <span class="hljs-keyword">for</span>($i=<span class="hljs-number">0</span>;$i&lt;$this-&gt;nums;$i++){
            $font_size = rand($this-&gt;font_size[<span class="hljs-string">&apos;min&apos;</span>],$this-&gt;font_size[<span class="hljs-string">&apos;max&apos;</span>]);
            $color = imagecolorallocate($this-&gt;pic,rand(<span class="hljs-number">127</span>,<span class="hljs-number">255</span>),rand(<span class="hljs-number">127</span>,<span class="hljs-number">255</span>),rand(<span class="hljs-number">127</span>,<span class="hljs-number">255</span>));
            imagettftext($this-&gt;pic,$font_size,rand(<span class="hljs-number">-15</span>,<span class="hljs-number">15</span>),$ww*$i+<span class="hljs-number">7</span>,rand($hh,$hh+<span class="hljs-number">20</span>),$color,$this-&gt;font_file,$fonts[$i]);
        }
    }
    <span class="hljs-comment">//&#x83B7;&#x53D6;&#x9A8C;&#x8BC1;&#x7801;</span>
    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getCode</span><span class="hljs-params">()</span></span>{
        $this-&gt;createPic();
        $this-&gt;fillBg();
        $this-&gt;fillPixel();
        $this-&gt;fillLine();
        $this-&gt;fillText();
        <span class="hljs-keyword">return</span> imagepng($this-&gt;pic);
    }
}
header(<span class="hljs-string">&apos;Content-Type:image/png&apos;</span>);
$c = <span class="hljs-keyword">new</span> Code();
$c-&gt;getCode();
</code></pre>
<h2 id="&#x6C34;&#x5370;&#x56FE;">&#x6C34;&#x5370;&#x56FE;</h2>
<h2 id="&#x7F29;&#x7565;&#x56FE;">&#x7F29;&#x7565;&#x56FE;</h2>
<h2 id="&#x63A8;&#x8350;">&#x63A8;&#x8350;</h2>
<p><a href="http://blog.csdn.net/morewindows/article/details/7274870" target="_blank">PHP&#x753B;&#x56FE;&#x57FA;&#x7840;</a></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="05_session.html" class="navigation navigation-prev " aria-label="Previous page: session">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="07_php加密机制.html" class="navigation navigation-next " aria-label="Next page: php加密机制">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"php画图","level":"1.3.5","depth":2,"next":{"title":"php加密机制","level":"1.3.6","depth":2,"path":"php/php高级/07_php加密机制.md","ref":"./php/php高级/07_php加密机制.md","articles":[]},"previous":{"title":"session","level":"1.3.4","depth":2,"path":"php/php高级/05_session.md","ref":"./php/php高级/05_session.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"php/php高级/06_php画图.md","mtime":"2020-04-23T10:29:27.114Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-04-23T11:06:38.883Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

